package org.renhj.provider.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;
import org.renhj.common.RpcRequest;
import org.renhj.common.RpcResponse;
import org.renhj.provider.handler.RpcProviderHandler;
import org.renhj.register.Register;
import org.renhj.register.ZookeeperRegister;
import org.renhj.rpc.RpcDecoder;
import org.renhj.rpc.RpcEncoder;

import java.net.Inet4Address;
import java.net.UnknownHostException;

public class RpcServer {


    private final int port;


    private final NioEventLoopGroup bossGroup;
    private final NioEventLoopGroup workerGroup;
    private final ServerBootstrap serverBootstrap;
    private final Register register = ZookeeperRegister.getInstance();



    public RpcServer(int port){
        this.port = port;
        bossGroup = new NioEventLoopGroup();
        workerGroup = new NioEventLoopGroup();

        serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        ChannelPipeline pipeline = socketChannel.pipeline();
                        pipeline.addLast(new RpcEncoder(RpcResponse.class));
                        pipeline.addLast(new RpcDecoder(RpcRequest.class));
                        pipeline.addLast(new RpcProviderHandler());
                    }
                });
    }


    public void startServer() throws Exception {
        try {
            ChannelFuture sync = serverBootstrap.bind(port).sync();

            startRegister(Inet4Address.getLocalHost().getHostAddress(), port);

            sync.channel().closeFuture().sync();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            deleteRegister(Inet4Address.getLocalHost().getHostAddress(), port);
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }


    public void stopServer() throws Exception {
        System.out.println("关闭服务器");
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
        deleteRegister(Inet4Address.getLocalHost().getHostAddress(), port);
    }

    private void startRegister(String address, int port) throws Exception {
        // 只注册userService
        register.register("UserService", address + ":" + port );
    }

    private void deleteRegister(String address, int port) throws Exception {
        register.release("UserService", address + ":" + port);
    }

}
